package course;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import course.method;
/**
 * Servlet implementation class Minis
 */
/* @WebServlet("/Minis") */
public class Minis extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Minis() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
		 PrintWriter out=response.getWriter();
		 
		 String isbn = request.getParameter("number"); 
		 if(method.Connection("INSERT INTO bookselect SELECT ISBN,bookname,writer,publisher,booktime FROM booklist WHERE ISBN= "+'"'+isbn+'"')){
			 method.Connection("UPDATE bookselect SET booktime = DATE_ADD(NOW(),INTERVAL 3 MONTH) WHERE ISBN="+'"'+isbn+'"');
         	 method.Connection("UPDATE booklist SET store=store-1 WHERE ISBN="+'"'+isbn+'"'); 
         	 JOptionPane.showMessageDialog(null, "借阅成功"); 
         	 response.sendRedirect("index.jsp");
           }
           else{
         	  JOptionPane.showMessageDialog(null, "您已借阅，请按时归还"); 
         	 response.sendRedirect("index.jsp");
           }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
